package mule.presenter;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import mule.model.GameModel;
import mule.model.Player;
import mule.model.enums.Race;
import mule.view.PlayerOptionView;

/**
 *
 * @author stephen
 */
public class PlayerOptionPresenter implements Presenter
{
    private final GameModel model;
    private final PlayerOptionView view;
    private final Stage stage;
    private int playerNumber;

    public PlayerOptionPresenter( Stage stage, int playerNumber )
    {
        this.playerNumber = playerNumber;
        this.stage = stage;
        this.model = GameModel.getInstance();
        this.view = new PlayerOptionView( playerNumber );
    }

    @Override
    public void present() {
        stage.setScene( view.getScreen() );
        addListeners();
        stage.show();
    }

    @Override
    public void addListeners() 
    {
    view.addContinueListener( new EventHandler<ActionEvent>() 
        {
            @Override
            public void handle( ActionEvent event ) 
            {
                String name = view.getPlayerName();
                Race race = view.getPlayerRace();
                Color color = view.getPlayerColor();
                Player thisPlayer = new Player( name, race, color );
                model.addPlayerToArray( thisPlayer );

                if( playerNumber == 1 )
                {
                    model.setTurn( thisPlayer );
                }

                playerNumber += 1;
                if( playerNumber <= model.getNumPlayers() )
                {
                    PlayerOptionPresenter playerOptionPresenter = new PlayerOptionPresenter( stage, playerNumber );
                    playerOptionPresenter.present();
                }
                else
                {
                    GamePresenter gamePresenter = new GamePresenter( stage );
                    gamePresenter.present();
                }
            } 
        } );
    }

}
